var allowance_city;
	//未报名状态
	var allowance = {
		url: "/allowance",
		className: "allowance",
		render: function () {
			allowance_city = getCookie("city");
			if(allowance_city){
				allowance_city=JSON.parse(allowance_city);
			}else{
				allowance_city=null;
			}
			return setTitle("钻石"), template("tpl_allowance", {url:"goback",city:allowance_city,nav:2})
		},
		bind: function () {
			var _this=this;
			$(window).unbind("scroll");
			//进入之后也判断一下，避免错误 
			ajaxGet("firework/schedule/issigup.json",{type:1},function(data){
				if(data.check){
					window.location.replace("/firework/bdk/diamond.html");
				}
			})
			if(!allowance_city){
				//根据ip，获取默认城市
				ajaxGet('utils/ip/city.json', {}, function (data) {
					if (data.code === 200) {
						$(_this).find("#selected").html(data.data.city);
						allowance_city={id:data.data.city_id,name:data.data.city};
						//也存入cookie中
						getCitySchedule(allowance_city.id);
					}
				})
			}else{
				getCitySchedule(allowance_city.id);
			}
			//立即报名
			$("#city_result").on('click','.btn_join', function () {
				var id=$(this).data("id");
				ajaxPost('firework/schedule/signup.json', {id:id}, function (data) {
					if (data.code === 200) {
						showInfoMsg('报名成功');
						window.location.replace("/firework/bdk/diamond.html");
					} else {
						showErrorMsg('报名失败');
					}
				},function(data){
					showErrorMsg(data.message || '报名失败');
				})
			});
			function getCitySchedule(cityid){
				ajaxGet("firework/schedule/findbycity.json",{id:cityid},function(data){
					$(_this).find("#city_result").html(template("tpl_allowance_data",data));
				},function(){
					//暂无数据
				})
			}
		}
	};
	router.push(allowance);
	//已完成审核，进入纯收入阶段
	var allowanceadmin={
		url: "/allowanceadmin",
		className: "allowanced",
		render: function () {
			return setTitle("钻石"), template("tpl_allowanced", {nav:2})
		},
		bind: function () {
			var _this=this;
			//查询代理的区域
			ajaxGet("firework/schedule/scheduleInfo.json",{type:1},function(data){
				$(_this).find("#head").html(template("tpl_allowanced_admin_head",data.data));
				/*if(data.data.stype==1){
					$(_this).find("#prop").html("<a href='#/luckyadmin' class='weui-btn weui-btn_default btn_admin'>管理</a>")
				}*/
				//$(_this).find("#prop").append(template("tpl_allowanced_prop",{}));
			},function(){
				//没有数据，跳转到申请界面
				go("/firework/bdk/diamond.html");
			});
			ajaxGet("firework/schedule/applyCounts.json",{},function(data){
				$(_this).find("#head").after(template("tpl_luckyadmin", data));
				tabPullLoadData(_this, '#money_tab .weui-navbar__item', 
				'#tab_panel_0 .weui-media-box_text',
				[base_url+"firework/schedule/applying.json",
				base_url+"firework/schedule/scheding.json",
				base_url+"firework/schedule/adopted.json"],
				["tpl_luckyadmin_data",
				"tpl_luckyadmin_scheding_data",
				"tpl_luckyadmin_adopted_data"],
				{},function(data,tabPageName,itemIndex){
					console.log(tabPageName,itemIndex)
				},"get","weui-bar__item_on");
			});
			
		}};
	router.push(allowanceadmin);
	var luckyadmin={
		url: "/luckyadmin",
		className: "luckyadmin",
		render: function () {
			//获取统计数据
			return new Promise(function (resolve, reject){
				setTitle("幸运宝店客");
				ajaxGet("firework/schedule/applyCounts.json",{},function(data){
					data["nav"]=2;
					resolve(template("tpl_luckyadmin", data));
				});
	        });
		},
		bind: function () {
			var _this=this;
			tabPullLoadData(_this, '#money_tab .weui-navbar__item', 
				'#tab_panel_0 .weui-media-box_text',
				[base_url+"firework/schedule/applying.json",
				base_url+"firework/schedule/scheding.json",
				base_url+"firework/schedule/adopted.json"],
				["tpl_luckyadmin_data",
				"tpl_luckyadmin_scheding_data",
				"tpl_luckyadmin_adopted_data"],
				{},function(data,tabPageName,itemIndex){
					console.log(tabPageName,itemIndex)
				},"get","weui-bar__item_on");
		}};
	router.push(luckyadmin);
	// 幸运客信息
	var applyid=0;
	var luckyinfo={
		url: "/luckyinfo/:id",
		className: "luckyadmin",
		render: function () {
			//获取审核信息
			applyid=this.params.id;
			return new Promise(function (resolve, reject){
				setTitle("审核幸运宝店客");
				ajaxGet("firework/schedule/applyInfo.json",{id:applyid},function(data){
					resolve(template("tpl_luckyinfo", data.data));
				});
	        });
			//return setTitle("幸运宝店客"), template("tpl_luckyadmin", {})
		},
		bind: function () {
			var _this=this;
			ajaxGet("firework/schedule/auditingText.json",{},function(data){
				$(_this).find("#refuse").html(template("tpl_luckyaudit",data));
			});
			$(_this).find("#btn_submit").click(function(){
				var result = $(_this).find("input[name='auditing']:checked").val();
				ajaxPost("firework/schedule/submitAudit.json",{id:applyid,result:result},function(data){
					goback(-1)
				},function(data){
					showErrorMsg(data.message || "提交失败");
				})
			})
			
		}};
	router.push(luckyinfo);
	var bdk_schedule_id=0;
	//报名成功，开始考核
	var allowanced={
		url: "/allowanced/:id",
		className: "allowanced",
		render: function () {
			bdk_schedule_id=this.params.id;
			return setTitle("钻石"), template("tpl_allowanced", {nav:2})
		},
		bind: function () {
			var _this=this;
			//查询代理的区域
			ajaxGet("firework/schedule/scheduleProcess.json",{id:bdk_schedule_id},function(data){
				$(_this).find("#head").html(template("tpl_allowanced_head",data.data));
				var bar = new ProgressBar.SemiCircle("#process", {
					  strokeWidth: 4,
					  color: '#FF0000',
					  trailColor: 'rgba(255, 255, 255, 0.38)',
					  trailWidth: 4,
					  easing: 'easeInOut',
					  duration: 1400
					});
				bar.animate(data.data.proc);  // Number from 0.0 to 1.0
				
				//购买补贴卡
				$(_this).find("#prop").html(template("tpl_allowanced_prop",data.data));
				var ctx = document.getElementById('canvas').getContext('2d');
				ajaxGet("firework/schedule/scheduleProcessDays.json",{id:bdk_schedule_id},function(data){
					var pro=data.data;
					var color = Chart.helpers.color;
					var barChartData = {
						labels: pro.days,
						datasets: [{
							backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
							data: pro.numbers
						}]
					};
					Chart.defaults.global["defaultFontColor"]="#A8BBCB";
					//http://www.chartjs.org/
					new Chart(ctx, {
						type: 'bar',
						data: barChartData,
						options: chartOptions
					});
				},function(){
				});
			},function(){
				//没有数据，跳转到申请界面
				//go("#/allowance");
			});
		}
	};
	router.push(allowanced);
	//幸运宝店客报名成功，开始考核
	var allowancedLucky={
		url: "/allowancedlucky/:id",
		className: "allowanced",
		render: function () {
			bdk_schedule_id=this.params.id;
			return setTitle("宝店客"), template("tpl_allowanced", {nav:3})
		},
		bind: function () {
			var _this=this;
			$(_this).addClass("withLucky");
			//查询代理的区域
			ajaxGet("firework/schedule/scheduleProcess.json",{id:bdk_schedule_id},function(data){
				$(_this).find("#head").html(template("tpl_allowanced_lucky_head",data.data));
				var bar = new ProgressBar.SemiCircle("#process", {
					  strokeWidth: 4,
					  color: '#FF0000',
					  trailColor: 'rgba(255, 255, 255, 0.38)',
					  trailWidth: 4,
					  easing: 'easeInOut',
					  duration: 1400
					});
				bar.animate(data.data.proc);  // Number from 0.0 to 1.0
				//购买补贴卡
				$(_this).find("#prop").html(template("tpl_allowanced_prop",data.data)).append("<div class='minfo'>"+data.data.moreinfo+"</div>");
				var ctx = document.getElementById('canvas').getContext('2d');
				ajaxGet("firework/schedule/scheduleProcessDays.json",{id:bdk_schedule_id},function(data){
					var pro=data.data;
					var color = Chart.helpers.color;
					var barChartData = {
						labels: pro.days,
						datasets: [{
							backgroundColor: color(window.chartColors.read).alpha(0.5).rgbString(),
							data: pro.numbers
						}]
					};
					Chart.defaults.global["defaultFontColor"]="#A18732";
					//http://www.chartjs.org/
					new Chart(ctx, {
						type: 'bar',
						data: barChartData,
						options: chartOptions
					});
				},function(){
				});
			},function(){
				//没有数据，跳转到申请界面
				//go("#/allowance");
			});
		}
	};
	router.push(allowancedLucky);
	var baocard={
		url: "/baocard",
		className: "baocard",
		render: function () {
			return setTitle("宝店补贴卡"), template("tpl_card", {})
		},
		bind: function () {
			var _this=this;
			var cards={};
			$(_this).find("#index_hype_container").append('<script type="text/javascript" charset="utf-8" src="//cdn6.mofing.com/animate/baodianke/index_hype_generated_script.js?45310"></script>')
			ajaxGet('propcard/props/baodianbutie.json',{},function(data){
				$(_this).find("#cards").html(template("tpl_card_data",data));
				$.each(data.data, function(ind,ele) {
					cards[ele.id]={
						id:ele.id,
						price:ele.price,
						counts:0
					};
				});
			});
			$(_this).find("#cards").on("click",".btn_mie",function(){
				//减少数量
				var nub = parseInt($(this).next().val());
				nub--;
				if(nub<=0){
					nub=0;
					$(this).parent().addClass("nonub")
				}
				$(this).next().val(nub);
				var cardid = $(this).parent().data("id");
				cards[cardid]["counts"]=nub;
			}).on("click",".btn_add",function(){
				//增加数量
				var prv= $(this).prev();
				var nub = parseInt(prv.val());
				var max= parseInt(prv.attr("max"));
				//如果nub>=10 则步长为10
				if(nub>9){
					nub=nub+10;
				}else{
					nub++;
				}
				if(nub >= max){
					nub=max;
				}else if(nub==1){
					$(this).parent().removeClass("nonub")
				}
				prv.val(nub);
				var cardid = $(this).parent().data("id");
				cards[cardid]["counts"]=nub;
			}).on("click",".btn_select",function(){
				//选择不限制次数的道具
				var cardid = $(this).parent().data("id");
				if($(this).parent().hasClass("nonub")){
					$(this).parent().removeClass("nonub");
					cards[cardid]["counts"]=1;
				}else{
					$(this).parent().addClass("nonub");
					cards[cardid]["counts"]=0;
				}
			}).on("click",".btn_nub",function(){
				//计算总价
				var sum=0;
				$.each(cards, function(ind,ele) {
					sum+=ele.price*ele.counts;
				});
				$(_this).find("#total").html(sum);
				return false;
			}).on("click",".img",function(){
				//点击图片，相当于增加一次数量
				$(this).parent().find(".btn_add,.btn_select").click()
			});
			//支付按钮
			$(_this).find("#btn_pay").click(function(){
				//判断是否选择了道具
				var props=[];
				$.each(cards, function(ind,ele) {
					if(ele.counts>0){
						props.push(ele);
					}
				});
				if(props.length>0){
					ajaxPost('propcard/orders/add.json',{card:props},function(data){
						console.log(data)
						//调用支付接口
						payOrder(data.data,'Propcard.order').then(function(paydata){
							console.log(paydata);
							//go("#/order/"+data.payid+"/"+data.id)
							//支付成功回调
						},function(){
							//支付失败回调
						})
					},function(){
						showErrorMsg("提交失败")
					});
				}
			})
		}
	};
	router.push(baocard);